/* 
 * Copyright 2012 Share.Ltd  All rights reserved.
 * Share.Ltd PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * 
 * @JsonUtil.java - 2013-2-26 ����2:06:29 - anonymous
 */

package com.util;

import java.lang.reflect.Field;

import org.json.JSONObject;

import com.alibaba.fastjson.JSON;

public class JsonUtil1 extends JSON {

	@SuppressWarnings("unchecked")
	public static <E> E getObject(String jsonString, Class<E> clazz) {

		return (E) JSON.parseObject(jsonString, clazz);
	}

	public static String getJsonString(Object object) {
		return JSON.toJSONString(object);
	}

	
	
	/* * 将对象转换成json */
	 
	public static String toJSon(Object o) throws Exception {
	 
	          JSONObject jo = new JSONObject();
	 
	           Field[] fields = o.getClass().getFields();
	 
	           for (Field f : fields) {
	 
	                  if (f.isAnnotationPresent(JSONValue.class)) {
	 
	                          JSONValue jv = f.getAnnotation(JSONValue.class);
	 
	                           String tag = jv.name();// System.out.println("tag--->>>"+tag);
	 
	                           if (tag.length() > 0) {
	                                       if (f.getType().getSimpleName().equals("int")) {
	 
	                                                jo.put(tag, f.getInt(o));
	 
	                                       } else {
	 
	                                             Object  v = f.get(o);
	 
	                                             if (v != null)jo.put(tag, v);
	 
	                                       }
	 
	                            }
	 
	                     }
	 
	                 }
	 
	                   System.out.println("tojson--->" + jo.toString());return jo.toString();
	 
	         }
	 
}



